<template>
  <div class="auther_table_wrap">
    <el-table
      :data="specs"
      border
      style="width: 95.2%; margin: 0px auto"
      size="medium"
    >
      <el-table-column prop="id" label="id" width="100"> </el-table-column>
      <el-table-column prop="name" label="规格名称"> </el-table-column>
      <el-table-column prop="spu_name" label="SPU商品名称"> </el-table-column>
      <el-table-column prop="spu" label="SPU商品id"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="fnPopShow(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="fnDelSpecs(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改规格" :visible.sync="pop_show" append-to-body>
      <el-form
        :model="specsForm"
        status-icon
        :rules="rulesSpecsForm"
        ref="specsForm"
        label-width="100px"
      >
        <el-form-item label="规格名称：" prop="name">
          <el-input
            type="text"
            v-model="specsForm.name"
            autocomplete="off"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="SPU：" prop="spu_id">
          <el-select v-model="specsForm.spu_id" size="small">
            <el-option
              v-for="item in spus_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm('specsForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import cons from "@/components/constant";
let token = localStorage.token;
export default {
  name: "AuthorityTable",
  props: ["specs"],
  data() {
    var validateName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("规格名不能为空"));
      } else {
        callback();
      }
    };
    return {
      pop_show: false,
      edit_id: "",
      spus_list: [],
      specsForm: {
        name: "",
        spu_id: "",
      },
      rulesSpecsForm: {
        name: [{ validator: validateName, trigger: "blur" }],
      },
    };
  },
  methods: {
    // 点击编辑触发的方法
    fnPopShow(id) {
      this.pop_show = true;
      this.edit_id = id;
      this.axios
        .get(cons.apis + "/goods/spuSpecs/" + this.edit_id + "/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          console.log("点击编辑触发的方法:", dat);
          this.specsForm.name = dat.data.name;
          this.specsForm.spu_id = dat.data.spu;
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    // 提交
    submitForm() {
      this.axios
        .put(
          cons.apis + "/goods/spuSpecs/" + this.edit_id + "/",
          {
            name: this.specsForm.name,
            spu: this.specsForm.spu_id,
          },
          {
            headers: {
              Authorization: "JWT " + token,
            },
            responseType: "json",
          }
        )
        .then((dat) => {
          console.log("更新规格的响应", dat);
          this.pop_show = false;
          this.$emit("fnResetTable");
          this.resetForm("specsForm");
          if (dat.status == 200) {
            this.$message({
              type: "success",
              message: "修改规格成功!",
            });
          } else {
            this.$message({
              type: "error",
              message: "修改规格失败!",
            });
          }
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    fnDelSpecs(id) {
      this.edit_id = id;
      this.$confirm("此操作将删除该规格, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
      
        .then(() => {
          this.axios
            .delete(cons.apis + "/goods/spuSpecs/" + this.edit_id + "/", {
              headers: {
                Authorization: "JWT " + token,
              },
              responseType: "json",
            })
            .then((dat) => {
              console.log('这是删除spu规格的响应', dat)
              // this.$message({
              //   type: "success",
              //   message: dat.data.msg,
              // });
              // this.$emit("fnResetTable");

            if (dat.data.status == 201) {
            this.$message({
                type: "success",
                message: dat.data.msg,
              });

            this.$emit("fnResetTable");
          }else{
                this.$message({
                type: "error",
                message: dat.data.msg,
              });

            this.$emit("fnResetTable");
          }
            })
            .catch((err) => {
              if (err.response.status == 404) {
                this.$message({
                  type: "info",
                  message: "商品未找到！",
                });
              }
            });
        })
        // .then(() => {
        //   this.axios
        //     .delete(cons.apis + "/goods/spuSpecs/" + this.edit_id + "/", {
        //       headers: {
        //         Authorization: "JWT " + token,
        //       },
        //       responseType: "json",
        //     })
        //     .then((dat) => {
        //       this.$message({
        //         type: "success",
        //         message: "删除成功!",
        //       });
        //       this.$emit("fnResetTable");
        //     })
        //     .catch((err) => {
        //       if (err.response.status == 404) {
        //         this.$message({
        //           type: "info",
        //           message: "规格未找到！",
        //         });
        //       }
        //     });
        // })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取所有的规格数据
    fnGetSpus() {
      this.axios
        .get(cons.apis + "/goods/spus/", {
          headers: {
            Authorization: "JWT " + token,
          },
          responseType: "json",
        })
        .then((dat) => {
          this.spus_list = dat.data;
        })
        .catch((err) => {
          console.log(err.response);
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    this.fnGetSpus();
  },
};
</script>

<style scope>
.el-table th,
.el-table td {
  text-align: center;
}
</style>
